const CCDownload = uni.requireNativePlugin('Bokecc-CcVodUniPlugin-CCDownload');

const {
	platform
} = uni.getSystemInfoSync();


const Downloader = {

	async getSidWithRetry(data, retryTime, successCallback, failCallback) {
		let _this = this
		try {
			const res = await getSid(data)
			if (!res.sdkSid) {
				throw Error('下载获取sid失败')
			} else {
				console.log(`在还有${retryTime}次重试机会时, sid请求成功`)
				successCallback && successCallback(res)
				return
			}
			return res
		} catch (error) {
			if (retryTime <= 0) {
				console.error('重试结束，下载获取sid失败')
				failCallback && failCallback()
				return
			}
			console.error(`下载获取sid请求失败，剩余重试次数${retryTime}次`)
			return setTimeout(() => {
				_this.getSidWithRetry(data, retryTime - 1, successCallback, failCallback)
			}, 1000)
		}
	},
	setSdkSid(uid, sid) {
		console.log('setSdkSid', sid);
		try {
			if (platform === 'ios') {
				CCDownload.setSdkSid(sid);
			} else {
				CCDownload.setSdkSid(uid, sid);
			}
		} catch (e) {
			console.log(e);
		}
	},
	init(params, successCallback) {
		console.log('downloader init', params);
		let _this = this
		if (platform === 'android') {
			// 安卓获取离线视频路径
			CCDownload.getFilePath();
			if (Array.isArray(params)) {
				// 多账号
				let list = []
				// params.forEach(item=> {
				// 	if (!item.apiKey) {
				// 		const data = {
				// 			uid: item.userId
				// 		}
				// 		let retryTime = 5;
				// 		let successCallback = (res) => {
				// 			_this.setSdkSid(data.uid, res.sdkSid)
				// 			const getSidTimer = setTimeout((res) => {
				// 				_this.getSidWithRetry(data, retryTime, successCallback)
				// 			}, res.expireSeconds * 1000)
				// 		}
				// 		const p = _this.getSidWithRetry(data, retryTime, successCallback)
				// 		list.push(p)
				// 	}
				// })
				// if (list.length) {
				// 	Promise.all(list).then((values) =>{
				// 		console.log(values)
				// 		CCDownload.init({
				// 			userId: params[0].userId,
				// 			apiKey: params[0].apiKey
				// 		});
				// 		successCallback()
				// 	}).catch(e => {
				// 		console.log(e)
				// 	})
				// } else {
				// 	CCDownload.init({
				// 		userId: params[0].userId,
				// 		apiKey: params[0].apiKey
				// 	});
				// 	successCallback()
				// }
				CCDownload.init({
					userId: params[0].userId,
					apiKey: params[0].apiKey
				});
				successCallback()
			} else {
				// 单账号
				// if (!params.apiKey) {
				// 	let data = {
				// 		uid: params.userId
				// 	}
				// 	let retryTime = 5;
				// 	let getSidSuccessCallback = (res) => {
				// 		CCDownload.init(params);
				// 		successCallback()
				// 	}
				// 	this.getSidWithRetry(data, retryTime, getSidSuccessCallback)
				// } else {
				// 	CCDownload.init(params);
				// 	successCallback()
				// }
				CCDownload.init(params);
				successCallback()
			}
		} else {
			CCDownload.initDownloadManager();
			successCallback()
		}
	},
	start(params) {
		let _this = this
		let checkTag = platform === 'ios' ? params : params[0]
		// if (!checkTag.apiKey) {
		// 	let data = {
		// 		uid: checkTag.userId
		// 	}
		// 	let retryTime = 5;
		// 	_this.getSidWithRetry(data, retryTime, (res) => {
		// 		_this.setSdkSid(data.uid, res.sdkSid)
		// 		const getSidTimer = setTimeout(() => {
		// 			_this.getSidWithRetry(data, retryTime)
		// 		}, res.expireSeconds * 1000)
		// 		CCDownload.startWithDownload(params);
		// 	}, () => {
		// 		uni.showToast({
		// 			title: '下载获取sid失败, 请检查网络'
		// 		})
		// 	})
		// } else {
		// 	CCDownload.startWithDownload(params);
		// }
		CCDownload.startWithDownload(params);
	},
	update() {
		CCDownload.update();
	},
	pause(params) {
		console.log('downloader pause')
		try {
			CCDownload.setDownloadPause(params)
		} catch (e) {
			console.log('pause error:', e);
		}
	},
	resume(params) {
		console.log('downloader resume')
		try {
			CCDownload.setDownloadResume(params)
		} catch (e) {
			console.log('resume error:', e);
		}
	},
	delete(params) {
		try {
			CCDownload.setDownloadDelete(params);
			console.log('删除成功');
		} catch (e) {
			console.log('delete error:', e);
		}
	},
	finishedDelete(params) {
		try {
			CCDownload.setDownloadFinishedDelete(params);
			console.log('安卓已下载视频删除成功');
		} catch (e) {
			console.log('delete error:', e);
		}
	}
}

export default Downloader;